package aes.gui;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import aes.general.TeacherControl;


/**
 * The Class TeacherApproveGradePanel. This panel allows the teacher to check the exams that has been finished by 
 * the students. The teacher approves the grades and can also change them if necessary.
 */
public class TeacherApproveGradePanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel jp;
	private JPanel TablePanel;
	private JLabel l1;
	private JLabel l2;
	private JSeparator separator;
	private JComboBox examsBox; 
	private TableModel data;
	private JTable table = null;
	
	private ListSelectionModel lsm;
	private ArrayList<String> NoneApproved;
	private Vector<Vector<String>> info;
	private ArrayList<String> infotoapprove;
	private ArrayList<String> changes = new ArrayList<String>(); 
	
	
	/**
	 * Instantiates a new teacher approve grade panel.
	 * 
	 * @param contentPane
	 *            the content pane
	 */
	public TeacherApproveGradePanel(JPanel contentPane) {
		this.jp = contentPane;
		initialize();
	}
	
	
	/**
	 * Initialize.
	 */
	private void initialize() {
		setLayout(null);
		
		separator = new JSeparator();
		separator.setBounds(10, 43, 620, 2);
		add(separator);
		
		l1 = new JLabel("Approve Exam");
		l1.setFont(new Font("Tempus Sans ITC", Font.BOLD, 21));
		l1.setBounds(235, 11, 169, 29);
		add(l1);
		
		l2 = new JLabel("Choose Exam To Approve:");
		l2.setBounds(37, 91, 184, 14);
		add(l2);
		
		TablePanel = new JPanel();
		TablePanel.setBounds(40, 121, 560, 231);
		add(TablePanel);
		
		examsBox = new JComboBox();
		examsBox.setBounds(235, 88, 82, 20);
		add(examsBox);
		examsBox.addItem("------");
		
		NoneApproved = TeacherControl.getNonappdExams();
		for(String id:NoneApproved)
			examsBox.addItem(id);
				
				examsBox.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						if(examsBox.getSelectedIndex()==0){
							;
						}
						else
						{	
							TeacherControl.getNotapproveInfo(examsBox.getSelectedItem());
							while((info = TeacherControl.getInfo()) == null);
							
							if(!info.get(0).contains("error")){
							data = new AbstractTableModel() {
								
								@Override
								public Object getValueAt(int rowIndex, int columnIndex) {
									// TODO Auto-generated method stub
									return info.get(rowIndex).get(columnIndex);
								}
								
								@Override
								public int getRowCount() {
									// TODO Auto-generated method stub
									return info.size();
								}
								
								@Override
								public int getColumnCount() {
									// TODO Auto-generated method stub
									return info.get(0).size();
								}
								public String getColumnName(int column){
									String[] title = {"Exame ID","Date","grade"};
									return title[column];
								}
							};
							TeacherControl.Setinfo(null);
							table = new JTable(data);
							table.updateUI();
							table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
						    table.setFillsViewportHeight(true);
						    lsm = table.getSelectionModel();
							JScrollPane grdscroll = new JScrollPane(table);
							grdscroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);	
							TablePanel.add("Center",grdscroll);
							
							TeacherApproveGradePanel app = new TeacherApproveGradePanel(jp);
							jp.add(app,"approve");
							TeacherControl.Setinfo(null);
							CardLayout cl = (CardLayout)(jp.getLayout());
							cl.show(jp,"app");
							data = null;	
						}
							else
							{
								TeacherControl.Setinfo(null);
								TeacherControl.SetnonAppdExams(null);
								CardLayout cl = (CardLayout)(jp.getLayout());
								 cl.show(jp,"mainPanel");
							}
						}
						
					}
				});
				
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				TeacherControl.Setinfo(null);
				TeacherControl.SetnonAppdExams(null);
				CardLayout cl = (CardLayout)(jp.getLayout());
				 cl.show(jp,"mainPanel");
			}
		});
		btnCancel.setBounds(399, 404, 116, 23);
		add(btnCancel);
		
		JButton btnApprove = new JButton("Approve");
		btnApprove.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				infotoapprove = new ArrayList<String>();
				infotoapprove.add((String) examsBox.getSelectedItem());
				lsm = table.getSelectionModel();
				lsm.isSelectionEmpty();
				int minIndex = lsm.getMinSelectionIndex();
                int maxIndex = lsm.getMaxSelectionIndex();
                int i;
                for (i = minIndex; i <= maxIndex+1; i++) {
                    if (lsm.isSelectedIndex(i)){ 
                    	infotoapprove.add(info.get(i).get(0));	
                    }
                }
               
                if(infotoapprove.size() != 1){
                	TeacherControl.InserApprove(infotoapprove);
                
                	JOptionPane.showMessageDialog(null, "OK,Exam Approved", "OK",
        					JOptionPane.INFORMATION_MESSAGE);
                }
                if(!lsm.isSelectionEmpty())
                	lsm.removeIndexInterval(0, maxIndex);
                else{
                	JOptionPane.showMessageDialog(null, "Error,No Selection", "Error",
    					JOptionPane.ERROR_MESSAGE);
                }
            	if(examsBox.getSelectedIndex()==0)
					System.out.println("Empty");
				else
				{
					TeacherControl.Setinfo(null);
					TeacherControl.getNotapproveInfo(examsBox.getSelectedItem());
					
					while((info = TeacherControl.getInfo()) == null);
					if(!info.get(0).contains("error")){
					data = new AbstractTableModel() {
						
						@Override
						public Object getValueAt(int rowIndex, int columnIndex) {
							// TODO Auto-generated method stub
							return info.get(rowIndex).get(columnIndex);
						}
						
						@Override
						public int getRowCount() {
							// TODO Auto-generated method stub
							return info.size();
						}
						
						@Override
						public int getColumnCount() {
							// TODO Auto-generated method stub
							return info.get(0).size();
						}
						public String getColumnName(int column){
							String[] title = {"Exame ID","Date","grade"};
							return title[column];
						}
					};
					TeacherControl.Setinfo(null);
					table = new JTable(data);
					table.updateUI();
					table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
				    table.setFillsViewportHeight(true);
				    lsm = table.getSelectionModel();
					JScrollPane grdscroll = new JScrollPane(table);
					grdscroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);	
					TablePanel.add("Center",grdscroll);
					
					TeacherApproveGradePanel app = new TeacherApproveGradePanel(jp);
					jp.add(app,"approve");
					CardLayout cl = (CardLayout)(jp.getLayout());
					cl.show(jp,"app");
					data = null;
					lsm = null;
				}
					else
					{
						TeacherControl.Setinfo(null);
						TeacherControl.SetnonAppdExams(null);
						CardLayout cl = (CardLayout)(jp.getLayout());
						 cl.show(jp,"mainPanel");
					}
				}
                
			}
		});
		btnApprove.setBounds(124, 404, 116, 23);
		add(btnApprove);
		
		JButton btnChangeGrade = new JButton("Change Grade");
		btnChangeGrade.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(table != null){
				lsm = table.getSelectionModel();
				if(examsBox.getSelectedIndex()==0 || lsm.isSelectionEmpty() ){
				
					JOptionPane.showMessageDialog(null, "Error,No Selection", "Error",
	    					JOptionPane.ERROR_MESSAGE);
				}
				else{
					int rowSelected = table.getSelectedRow();
					changes.add((String) examsBox.getSelectedItem());
					changes.add((String) table.getModel().getValueAt(rowSelected, 0));
					changes.add((String) table.getModel().getValueAt(rowSelected, 2));
					TeacherChangeGradePanel tcgp = new TeacherChangeGradePanel(jp,changes);
					jp.add(tcgp,"ChangeGrade");
					CardLayout cl = (CardLayout)(jp.getLayout());
					cl.show(jp,"ChangeGrade");
				}
				}
				else
				{
					JOptionPane.showMessageDialog(null, "Error,No Selection", "Error",
	    					JOptionPane.ERROR_MESSAGE);	
				}
			}
			
		});
		btnChangeGrade.setBounds(262, 404, 116, 23);
		add(btnChangeGrade);

	}
}
